
# generate appendix D2, table D2

library(rio) # to load data
library(tidyverse) # to reshape data
library(lmtest) # to cluster standard errors
library(sandwich) # to cluster standard errors
library(systemfit) # to run SUR
library(texreg) # to generate tables

# set your working directory
setwd("~/replication_files")

annual_data <- import("data/annual_data.csv") %>%
  select(iso3c,time,
         log_ratio_multilateral_bonds_delta,log_ratio_multilateral_bonds_lag,
         log_ratio_bilateral_bonds_delta,log_ratio_bilateral_bonds_lag,
         log_ratio_commercial_banks_bonds_delta,log_ratio_commercial_banks_bonds_lag,
         terms_of_trade_lag,terms_of_trade_delta,
         log_oil_production_lag,log_oil_production_delta,
         resource_rents_perc_lag,resource_rents_perc_delta,
         discovery_lag,
         debt_crisis_experience,
         minister_is_econ_technocrat_usa, 
         minister_turnover_5yrs,
         election_year, left_exec,
         fiscal_council,polcon,
         tax_perc_gdp_cepal_lag, 
         imf_agreement, 
         log_core_inflation_lag,
         fiscal_balance_lag,
         gdp_pc_cepal_lag, 
         gdp_growth_cepal_lag, 
         ka_open_lag, 
         log_reserves_lag,
         treasury_rate_lag) %>%
  drop_na() %>%
  fastDummies::dummy_cols() %>%
  mutate()

# table D2

# model 1
model1 <- log_ratio_multilateral_bonds_delta ~
  log_ratio_multilateral_bonds_lag +
  terms_of_trade_lag +
  terms_of_trade_delta +
  log_oil_production_lag + 
  log_oil_production_delta + 
  resource_rents_perc_lag + 
  resource_rents_perc_delta +
  discovery_lag +
  minister_is_econ_technocrat_usa +
  minister_turnover_5yrs +
  debt_crisis_experience +
  election_year + left_exec +
  fiscal_council + 
  polcon + 
  imf_agreement +
  fiscal_balance_lag + 
  tax_perc_gdp_cepal_lag +
  log_core_inflation_lag +
  gdp_pc_cepal_lag +
  gdp_growth_cepal_lag +
  ka_open_lag +
  log_reserves_lag +
  treasury_rate_lag + time + 
  iso3c_ARG + iso3c_BOL + iso3c_BRA + iso3c_COL + iso3c_CRI + iso3c_DOM +
  iso3c_ECU + iso3c_GTM + iso3c_GUY + iso3c_HND + iso3c_JAM + iso3c_MEX + 
  iso3c_PAN + iso3c_PER + iso3c_SLV

# model 2
model2 <- log_ratio_bilateral_bonds_delta ~
  log_ratio_bilateral_bonds_lag +
  terms_of_trade_lag +
  terms_of_trade_delta +
  log_oil_production_lag + 
  log_oil_production_delta + 
  resource_rents_perc_lag + 
  resource_rents_perc_delta +
  discovery_lag +
  minister_is_econ_technocrat_usa +
  minister_turnover_5yrs +
  debt_crisis_experience +
  election_year + left_exec +
  fiscal_council + 
  polcon + 
  imf_agreement +
  fiscal_balance_lag + 
  tax_perc_gdp_cepal_lag +
  log_core_inflation_lag +
  gdp_pc_cepal_lag +
  gdp_growth_cepal_lag +
  ka_open_lag +
  log_reserves_lag +
  treasury_rate_lag + time + 
  iso3c_ARG + iso3c_BOL + iso3c_BRA + iso3c_COL + iso3c_CRI + iso3c_DOM +
  iso3c_ECU + iso3c_GTM + iso3c_GUY + iso3c_HND + iso3c_JAM + iso3c_MEX + 
  iso3c_PAN + iso3c_PER + iso3c_SLV

# model 3
model3 <- log_ratio_commercial_banks_bonds_delta ~
  log_ratio_commercial_banks_bonds_lag +
  terms_of_trade_lag +
  terms_of_trade_delta +
  log_oil_production_lag + 
  log_oil_production_delta + 
  resource_rents_perc_lag + 
  resource_rents_perc_delta +
  discovery_lag +
  minister_is_econ_technocrat_usa +
  minister_turnover_5yrs +
  debt_crisis_experience +
  election_year + left_exec +
  fiscal_council + 
  polcon + 
  imf_agreement +
  fiscal_balance_lag + 
  tax_perc_gdp_cepal_lag +
  log_core_inflation_lag +
  gdp_pc_cepal_lag +
  gdp_growth_cepal_lag +
  ka_open_lag +
  log_reserves_lag +
  treasury_rate_lag + time + 
  iso3c_ARG + iso3c_BOL + iso3c_BRA + iso3c_COL + iso3c_CRI + iso3c_DOM +
  iso3c_ECU + iso3c_GTM + iso3c_GUY + iso3c_HND + iso3c_JAM + iso3c_MEX + 
  iso3c_PAN + iso3c_PER + iso3c_SLV

fitsur <- systemfit(list(multilateral = model1, bilateral = model2, banks = model3), data = annual_data, method = "SUR")

# generate table D2
texreg(fitsur, beside = TRUE, stars = c(0.01, 0.05, 0.1),
       custom.header = list("Dependent Variable:" = 1:3),
       custom.model.names = c("Ln (Multilateral to Bonds) $_{Delta}$",
                              "Ln (Bilateral to Bonds) $_{Delta}$",
                              "Ln (Commercial Banks to Bonds) $_{Delta}$"),
       custom.coef.map = list("log_ratio_multilateral_bonds_lag" = "Ln (Multilateral to Bonds) $_{t-1}$",
                              "log_ratio_bilateral_bonds_lag" = "Ln (Bilateral to Bonds) $_{t-1}$",
                              "log_ratio_commercial_banks_bonds_lag" = "Ln (Commercial Banks to Bonds) $_{t-1}$",
                              "resource_rents_perc_lag" = "Resource Rents, % of GDP $_{t-1}$",
                              "resource_rents_perc_delta" = "Resource Rents, % of GDP $_{Delta}$",
                              "log_oil_production_lag" = "Ln Oil and Gas Production $_{t-1}$",
                              "log_oil_production_delta" = "Ln Oil and Gas Production $_{Delta}$",
                              "terms_of_trade_lag" = "Commodity Price Index $_{t-1}$",
                              "terms_of_trade_delta" = "Commodity Price Index $_{Delta}$",
                              "discovery_lag" = "Field Discovery $_{t-1}$",
                              "minister_is_econ_technocrat_usa" = "Mainstream Minister = 1",
                              "minister_turnover_5yrs" = "Minister Turnover (5 Years)",
                              "debt_crisis_experience" = "Debt Crisis Experience = 1",
                              "election_month" = "Election Month = 1",
                              "left_exec" = "Left Executive = 1",
                              "cb_independence" = "Central Bank Independence",
                              "fiscal_council" = "Fiscal Council = 1",
                              "polcon" = "Political Constraints",
                              "imf_agreement" = "IMF Agreement = 1",
                              "fiscal_balance_lag" = "Fiscal Balance, % of GDP $_{t-1}$",
                              "tax_perc_gdp_cepal_lag" = "Tax Revenue, % of GDP $_{t-1}$",
                              "log_core_inflation_lag" = "Ln Core Inflation $_{t-1}$",
                              "gdp_pc_cepal_lag" = "GDP Per Capita $_{t-1}$",
                              "gdp_growth_cepal_lag" = "GDP Growth, % $_{t-1}$",
                              "ka_open_lag" = "Capital Openness $_{t-1}$",
                              "log_reserves_lag" = "Ln International Reserves $_{t-1}$",
                              "treasury_rate_lag" = "US Treasury Rate, % $_{t-1}$"#,
                              #"time" = "Time Trend",
                              #"(Intercept)" = "Constant"
       ),
       caption = "The Effect of Natural Resources on Sovereign Borrowing: Trade-Offs Between Different Creditors",
       fontsize = "footnotesize", digits = 3)



